#include "aky_program/aky_com_include.h"
static void aky_task_3_uart_decode(void);
static void aky_uart_3_send_byte(uint8_t data);

#define AKY_UART_3_BUF_MAX 50                      // 串口最大接收个数
static uint8_t aky_uart_3_buf[AKY_UART_3_BUF_MAX]; // 串口缓存
static uint16_t aky_uart_3_read_cnt = 0; // 一帧数据结束串口接收到的数据个数
static uint8_t aky_uart_3_time_out = 0;  // 超时处理  超时则将处理一帧数据标志位置1
static uint8_t flag_aky_uart_3_get_data = 0;
static uint8_t flag_aky_task_3_decode = 0; // 处理一帧数据标志位 置1代表接收到一帧数据


e_spk_recogni_t e_spk_recogni = SPK_UNDEF;
e_spk_recogni_t e_spk_recogni_info = SPK_UNDEF;

void UART3_Handler(void) // uart_3 中断
{
    uint32_t chr;
    if (UART_INTStat(UART3, UART_IT_RX_THR))
    {
        while (UART_IsRXFIFOEmpty(UART3) == 0)
        {
            if (UART_ReadByte(UART3, &chr) == 0)
            {
                aky_uart_3_buf[aky_uart_3_read_cnt] = chr;
                aky_uart_3_read_cnt++;
                if (aky_uart_3_read_cnt > AKY_UART_3_BUF_MAX) // 限制最大接收个数
                {
                    aky_uart_3_read_cnt = AKY_UART_3_BUF_MAX;
                }
                aky_uart_3_time_out = 0; // 超时时间清零
                flag_aky_uart_3_get_data = 1;
            }
            else
            {
                __NOP();
            }
        }
    }
    if (UART_INTStat(UART3, UART_IT_RX_TOUT))
    {
        UART_INTClr(UART3, UART_IT_RX_TOUT);
    }
}


void Timer4Callback(void) // 软件定时器 回调函数
{
    aky_uart_3_time_out++;                                        //
    if (aky_uart_3_time_out > 2 && flag_aky_uart_3_get_data == 1) // 超时着代表接收完一帧数据
    {
        aky_uart_3_time_out = 0;
        flag_aky_uart_3_get_data = 0;
        flag_aky_task_3_decode = 1; // 处理数据标志位置1
    }
}

static uint8_t *p_aky_uart_3_buf;
static void aky_task_3_uart_decode(void)
{
    uint8_t k, Checksum,cmd_code;
    uint8_t index = 0;

    if (flag_aky_task_3_decode == 1)
    {
        #if (AKY_PRINTF_TO_UART3 == 1)
        printf("rx3 decode\n");
        for (uint16_t i = 0; i < aky_uart_3_read_cnt; i++)
        {
            printf(" %02x", aky_uart_3_buf[i]);
        }
        printf("\n");
        #endif
        while (aky_uart_3_read_cnt > 1)
        {
            aky_uart_3_read_cnt--;
            p_aky_uart_3_buf = &aky_uart_3_buf[index];
            index++;
            if (p_aky_uart_3_buf[0] == 0x10)
            {
                Checksum = p_aky_uart_3_buf[0] + p_aky_uart_3_buf[1];
                if (Checksum == p_aky_uart_3_buf[2])
                {
                    cmd_code = p_aky_uart_3_buf[1];
                    if (cmd_code == 0x00)
                    {
                        e_spk_recogni = SPK_WAKE;
                        printf("SPK_WAKEr\n");
                    }
                    else if (cmd_code == 0x01)
                    {
                        e_spk_recogni = SPK_SHUTDOWN;
                        printf("SPK_SHUTDOWN\n");
                    }
                    else if (cmd_code == 0x05)
                    {
                        e_spk_recogni = SPK_BACK;
                        printf("SPK_BACK\n");
                    }
                    else if (cmd_code == 0x0F)
                    {
                        e_spk_recogni = VOICE_UP;
                    }       
                    else if (cmd_code == 0x10)
                    {
                        e_spk_recogni = VOICE_DOWN;
                    }        
                    else if (cmd_code == 0x11)
                    {
                        e_spk_recogni = VOICE_SHUT;
                    }                    
                    else if (cmd_code == 0x13)
                    {
                        e_spk_recogni = BL_UP;
                    }                    
                    else if (cmd_code == 0x14)
                    {
                        e_spk_recogni = BL_DOWN;
                    }     
                    else if (cmd_code == 0x02)
                    {
                        e_spk_recogni = START_AUTO_TRAIN;
                    }                      
                    else if (cmd_code == 0x03)
                    {
                        e_spk_recogni = START_THIS_TRAIN;
                    }           
                    else if (cmd_code == 0x04)
                    {
                        e_spk_recogni = ONE_CLICK_STOP;
                    }           
                    else if (cmd_code == 0x06)
                    {
                        e_spk_recogni = START_MANUAL_TRAIN;
                    } 
                    else if (cmd_code == 0x07)
                    {
                        e_spk_recogni = PLAN_1_TRAINING;
                    }           
                    else if (cmd_code == 0x08)
                    {
                        e_spk_recogni = PLAN_2_TRAINING;
                    }        
                    else if (cmd_code == 0x09)
                    {
                        e_spk_recogni = PLAN_3_TRAINING;
                    } 
                    else if (cmd_code == 0x0A)
                    {
                        e_spk_recogni = PLAN_4_TRAINING;
                    }    
                    else if (cmd_code == 0x0B)
                    {
                        e_spk_recogni = BP_ASSESSMENT;
                    }        
                    else if (cmd_code == 0x0C)
                    {
                        e_spk_recogni = BP_CHART;
                    } 
                    else if (cmd_code == 0x08)
                    {
                        e_spk_recogni = PLAN_2_TRAINING;
                    } 
                    else if (cmd_code == 0x0D)
                    {
                        e_spk_recogni = ENTER_SET;
                    } 
                    else if (cmd_code == 0x0e)
                    {
                        e_spk_recogni = VOICE_SET;
                    } 
                    else if (cmd_code == 0x12)
                    {
                        e_spk_recogni = BL_SET;
                    } 
                    else if (cmd_code == 0x15)
                    {
                        e_spk_recogni = NET_SET;
                    } 
                    else if (cmd_code == 0x16)
                    {
                        e_spk_recogni = NET_ON;
                    } 
                    else if (cmd_code == 0x17)
                    {
                        e_spk_recogni = NET_OFF;
                    } 
                    else if (cmd_code == 0x18)
                    {
                        e_spk_recogni = USE_TUTORIAL;
                    } 
                    else if (cmd_code == 0x19)
                    {
                        e_spk_recogni = VIDEO_TUTORIAL;
                    } 
                    else if (cmd_code == 0x1A)
                    {
                        e_spk_recogni = HEALTH_CONSULTATION;
                    } 
                    else if (cmd_code == 0x1B)
                    {
                        e_spk_recogni = POPULAR_SCIENCE_KNOWLEDGE;
                    } 
                    else if (cmd_code == 0x1C)
                    {
                        e_spk_recogni = WATCH_VIDEO_1;
                    } 
                    else if (cmd_code == 0x1D)
                    {
                        e_spk_recogni = WATCH_VIDEO_2;
                    }                 
                    else if (cmd_code == 0x1E)
                    {
                        e_spk_recogni = WATCH_VIDEO_3;
                    }                 
                    else if (cmd_code == 0x1F)
                    {
                        e_spk_recogni = WATCH_VIDEO_4;
                    }                 
                    else if (cmd_code == 0x20)
                    {
                        e_spk_recogni = WATCH_VIDEO_5;
                    }                 
                    else if (cmd_code == 0x21)
                    {
                        e_spk_recogni = WATCH_VIDEO_6;
                    }                 
                }
            }
        }
        
        flag_aky_task_3_decode = 0;
        aky_uart_3_read_cnt = 0;
    }
    //
}

void aky_task_3(void) // 任务入口
{
    Timer4Callback();
    aky_task_3_uart_decode();
    spk_recogni_pro();

}



void aky_uart_3_init(void)
{
    UART_InitStructure UART_initStruct;

    GPIO_Init(GPIOB, PIN4, 0, 1, 0, 0);
    PORT_Init(PORTB, PIN3, PORTB_PIN3_UART3_TX, 0); // GPIOx.x配置为UART引脚
    PORT_Init(PORTB, PIN4, PORTB_PIN4_UART3_RX, 1); // GPIOx.x配置为UART引脚

#if (AKY_PRINTF_TO_UART3 == 1)
    UART_initStruct.Baudrate = PRINTF_TO_UART3_BUAD; // 波特率 
#else
    UART_initStruct.Baudrate = 9600; // 波特率
#endif
    UART_initStruct.DataBits = UART_DATA_8BIT;
    UART_initStruct.Parity = UART_PARITY_NONE;
    UART_initStruct.StopBits = UART_STOP_1BIT;
    UART_initStruct.RXThreshold = 0;
    UART_initStruct.RXThresholdIEn = 1;
    UART_initStruct.TXThreshold = 3;
    UART_initStruct.TXThresholdIEn = 0;
    UART_initStruct.TimeoutTime = 10;
    UART_initStruct.TimeoutIEn = 0;
    UART_Init(UART3, &UART_initStruct);
    UART_Open(UART3);
}


// 发送一个字节
static void aky_uart_3_send_byte(uint8_t data)
{
    uint32_t time_out = 0;
    UART_WriteByte(UART3, data);
    while (UART_IsTXBusy(UART3))
    {
        time_out++;
        __NOP();
        if (time_out > 666666)
        {
            break;
        }
    }
}




void aky_task_3_init(void)
{
    aky_uart_3_init();
    clr_spk_recogni_info();
}

e_spk_recogni_t get_spk_recogni_info(void)
{
    return e_spk_recogni_info;
}

void clr_spk_recogni_info(void)
{
    e_spk_recogni_info = SPK_UNDEF;
}
typedef enum
{
    recogni_stat_idle = 0,
    recogni_stat_wake = 1,
    recogni_stat_get_cmd = 2,
}e_spk_recogni_stat_t;

e_spk_recogni_stat_t e_spk_recogni_stat = recogni_stat_idle;
void spk_recogni_pro(void)
{
    static uint32_t u32_tick_last = 0;

    switch (e_spk_recogni_stat)
    {
    case recogni_stat_idle:
    {
        if (e_spk_recogni == SPK_WAKE)
        {
            e_spk_recogni = SPK_UNDEF;
            e_spk_recogni_stat = recogni_stat_wake;
            e_spk_recogni_info = SPK_UNDEF;
            voice_play_single(WAKE_IAM);
            u32_tick_last = g_u32_tick_cnt;
            if (synwit_ui_get_cur_screen_id() == SCREEN015)
            {
                e_spk_recogni_info = SPK_WAKE;
            }
        }
        break;
    }
    case recogni_stat_wake:
    {
        if (g_u32_tick_cnt - u32_tick_last > 7000 / 10)
        {
            //一段时间没有其他指令 返回空闲
            u32_tick_last = g_u32_tick_cnt;
    
            e_spk_recogni = SPK_UNDEF;
            e_spk_recogni_stat = recogni_stat_idle;
            e_spk_recogni_info = SPK_UNDEF;
        }
        if (e_spk_recogni != SPK_UNDEF && e_spk_recogni != SPK_WAKE)
        {
            e_spk_recogni_info = e_spk_recogni;
            e_spk_recogni = SPK_UNDEF;
            e_spk_recogni_stat = recogni_stat_wake;
            u32_tick_last = g_u32_tick_cnt;
        }
        else if (e_spk_recogni == SPK_WAKE)
        {
            e_spk_recogni = SPK_UNDEF;
            voice_play_single(WAKE_IAM);
            u32_tick_last = g_u32_tick_cnt;
            if (synwit_ui_get_cur_screen_id() == SCREEN015)
            {
                e_spk_recogni_info = SPK_WAKE;
            }
        }
        
        break;
    }    
    default:
            e_spk_recogni = SPK_UNDEF;
            e_spk_recogni_stat = recogni_stat_idle;
            e_spk_recogni_info = SPK_UNDEF;
        break;
    }
    if (get_spk_recogni_info() == VOICE_UP)
    {
        if (synwit_ui_get_cur_screen_id() != SCREEN006 && synwit_ui_get_cur_screen_id() != SCREEN007 && synwit_ui_get_cur_screen_id() != SCREEN011 && synwit_ui_get_cur_screen_id() != SCREEN008)
        {
            clr_spk_recogni_info();
        }
        g_s_set.voice_level++;
        if (g_s_set.voice_level > VOICE_LEVEL_MAX)
        {
            g_s_set.voice_level = VOICE_LEVEL_MAX;
        }
    }
    if (get_spk_recogni_info() == VOICE_DOWN)
    {
        if (synwit_ui_get_cur_screen_id() != SCREEN006 && synwit_ui_get_cur_screen_id() != SCREEN007 && synwit_ui_get_cur_screen_id() != SCREEN011 && synwit_ui_get_cur_screen_id() != SCREEN008)
        {
            clr_spk_recogni_info();
        }
        if (g_s_set.voice_level > 0)
        {
            g_s_set.voice_level--;
        }
    }
    if (get_spk_recogni_info() == VOICE_SHUT)
    {
        if (synwit_ui_get_cur_screen_id() != SCREEN006 && synwit_ui_get_cur_screen_id() != SCREEN007 && synwit_ui_get_cur_screen_id() != SCREEN011 && synwit_ui_get_cur_screen_id() != SCREEN008)
        {
            clr_spk_recogni_info();
        }
        g_s_set.voice_level = 0;
    }

    if (get_spk_recogni_info() == BL_UP)
    {
        if (synwit_ui_get_cur_screen_id() != SCREEN006 && synwit_ui_get_cur_screen_id() != SCREEN007 && synwit_ui_get_cur_screen_id() != SCREEN011 && synwit_ui_get_cur_screen_id() != SCREEN008)
        {
            clr_spk_recogni_info();
        }
        g_s_set.bl_level++;
        if (g_s_set.bl_level > BL_LEVEL_MAX)
        {
            g_s_set.bl_level = BL_LEVEL_MAX;
        }
        set_bl_by_level(g_s_set.bl_level);
    }
    if (get_spk_recogni_info() == BL_DOWN)
    {
        if (synwit_ui_get_cur_screen_id() != SCREEN006 && synwit_ui_get_cur_screen_id() != SCREEN007 && synwit_ui_get_cur_screen_id() != SCREEN011 && synwit_ui_get_cur_screen_id() != SCREEN008)
        {
            clr_spk_recogni_info();
        }
        if (g_s_set.bl_level > 1)
        {
            g_s_set.bl_level--;
        }
        set_bl_by_level(g_s_set.bl_level);
    }
}
